<?php

/**
 * debate actions.
 *
 * @package    LYPO
 * @subpackage debate
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class debateActions extends sfActions
{
	public function preExecute(){
		if(sfConfig::get('sf_web_debug')==true){
			$this->setLayout('layout');
		}else{
			$this->setLayout(false);
		}
		$this->setTemplate('default');
	}
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeMain(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		$statement_id = $request->getParameter('statement_id');
		if(!$statement_id || !$userid){
			$result->setErrorMessage("No statement_id or userid given!");
			return $this->renderText(print_r($result->toArray(),1));
		}
		try {
			/* @var $user User */
			$user = UserPeer::getByContainerString($userid);
			if($user){
				$statement = LypoStatementPeer::retrieveByPK($statement_id);
				if(!$statement) throw new sfException('Statement niet gevonden!');
				$this->args = array('user' => $user, 'debate' => $statement, 'userid' => $userid, 'basehost' => $request->getHost());
				$this->partial = 'debate';

				$myanswer = StatementAnswerPeer::ANSWER_NOANSWER;
				
				if($statement){
					//Kijken of huidige gebruiker antwoord op heeft gegeven
					$answer = StatementAnswerPeer::getStatementAnswerForUser($statement,$user);
					if ($answer) $myanswer = $answer->getAnswer();
				}
				$this->args['myanswer'] = $myanswer;

			}else{
				$result->setErrorMessage("User not found");
				return $this->renderText(print_r($result->toArray(),1));
			}
		}catch( Exception $e){
			$result->setErrorMessage($e->getMessage());
			return $this->renderText(print_r($result->toArray(),1));
		}
	}

	public function executeReactions(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		$statement_id = $request->getParameter('statement_id');
		$view = $request->getParameter('view','all');
		try {
			/* @var $user User */
			$user = UserPeer::getByContainerString($userid);
			if($user){

				$this->args = array('user' => $user, 'userid' => $userid, 'basehost' => $request->getHost());
				$ret = $user->getPerfectOppositesIds(10);
				$this->args['opposite_ids'] = $ret['ids'];

				//Haal alle reactie-ids op waar gebruiker respect aan heeft gegeven
				$respected_reaction_ids = ReactionPeer::getRespectedReactionIdsForUser($user);
				$this->args['respected_reaction_ids'] = $respected_reaction_ids;
				
				switch($view){
					case "all":
						$this->partial = 'reactions_all';
						$all_reactions = ReactionPeer::getReactionsForStatement($statement_id);
						$this->args['all_reactions'] = $all_reactions;
						break;
					case "perfectopposites":
						$opposites_reactions = ReactionPeer::getPerfectOppositesReactionsForStatement($user, $statement_id);
						$this->args['opposites_reactions'] = $opposites_reactions;
						$this->partial = 'reactions_perfectopposites';
						break;
				}

				//return $this->renderPartial('debates');
			}else{
				$result->setErrorMessage("User not found");
				return $this->renderText(print_r($result->toArray(),1));
			}
		}catch( Exception $e){
			$result->setErrorMessage($e->getMessage());
			return $this->renderText(print_r($result->toArray(),1));
		}
	}
}
